package com.yiyuzi.admin.role;

import com.yiyuzi.admin.common.domain.IDQueryDTO;
import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.domain.ResponseDTO;
import com.yiyuzi.admin.role.domain.dto.*;
import com.yiyuzi.admin.role.domain.entity.RolePrivilegeEntity;
import com.yiyuzi.admin.role.service.RolePrivilegeService;
import com.yiyuzi.admin.role.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yiyuzi
 * @date 2021/5/8 11:32
 */

@RestController
@RequestMapping("rolePrivilege")
@Api(tags = "角色-权限")
public class RolePrivilegeController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePrivilegeService rolePrivilegeService;

    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询")
    public ResponseDTO<RolePrivilegeDTO> getById(@PathVariable Long id){
        return ResponseDTO.res(rolePrivilegeService.findById(id), ResponseCodeEnum.NOTFOUND);
    }

    @PostMapping("/query/all")
    @ApiOperation(value = "查询全部")
    public ResponseDTO<PageResultDTO<RolePrivilegeDTO>> getAll(){
        return ResponseDTO.res(rolePrivilegeService.findPage(new RolePrivilegeQueryDTO()), ResponseCodeEnum.NOTFOUND);
    }

    @PostMapping("/query/page")
    @ApiOperation(value = "条件查询")
    public ResponseDTO<PageResultDTO<RolePrivilegeDTO>> getPage(RolePrivilegeQueryDTO queryDTO){
        return ResponseDTO.res(rolePrivilegeService.findPage(queryDTO), ResponseCodeEnum.NOTFOUND);
    }

    @PostMapping("/add/one")
    @ApiOperation(value = "新增")
    public ResponseDTO<String> add(RolePrivilegeAddDTO addDTO){
        return ResponseDTO.res(rolePrivilegeService.add(addDTO));
    }

    @PostMapping("/add/list")
    @ApiOperation(value = "批量新增")
    @ResponseBody
    public ResponseDTO<String> add(@RequestBody List<RolePrivilegeAddDTO> list){
        try {
            return ResponseDTO.res(rolePrivilegeService.add(list));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public ResponseDTO<String> deleteById(@PathVariable Long id){
        return ResponseDTO.res(rolePrivilegeService.removeById(id));
    }

    @PostMapping("/delete/list")
    @ApiOperation(value = "批量删除")
    public ResponseDTO<String> deleteList(IDQueryDTO idQueryDTO){
        try {
            return ResponseDTO.res(rolePrivilegeService.deleteList(idQueryDTO.getIdList()));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }
}
